﻿using PBO.Data;
using PBO.Game.BattleClass.BattleState;
using PBO.Game.BattleLog;
using PokemonBattle.BattleNetwork;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace PBO.Game.BattleClass
{
    public class PokemonBattleState
    {

        #region static
        private static PokemonBattleState emptyState = new PokemonBattleState();

        public static PokemonBattleState EmptyBattleState
        {
            get { return emptyState; }
        }
        #endregion

        #region fields

        #region lv

        public int CTLV;

        public BattleStats AbilityLv = new BattleStats();

        #endregion

        #region public

        public bool afraid; //害怕状态
        public bool aquaRing;   //液体圈
        public bool assisted;   //帮手

        public bool choiceSleepTalked;

        public bool curse;  //诅咒
        public bool defenseCurl;    //变圆
        public bool destinyBond;    //殊途同归

        public bool firstTurnAtk = true;    //下马威

        public bool FocusEngery;
        public bool foresight;  //识破

        public bool gastricJuice;   //胃液
        public bool grudge; //怨念

        public bool healwish;   //治愈之愿
        public bool imprison;   //封印
        public bool ingrain;    //扎根
        public bool intimidated;    //威吓

        public bool lunarDance; //新月舞
        public bool magicCoat;  //魔装反射
        public bool meFirst;
        public bool minimize;   //变小
        public bool miracleEye; //奇迹之眼
        public bool moved;

        public string lastAttackAtMove;
        public Pokemon lastAttackAtMoveUser;

        public Move nextMove;
        public bool nextTurnCantMove;
        public bool nightmare;  //噩梦
        public bool pass;
        public bool powerTrick; //力量交换
        public bool prepare;
        public int previousMove;

        public bool pursuit;
        public bool quickBerry; //先制果
        public bool rage;   //愤怒
        public bool roost;  //鸟栖
        public int slowStartCounter;    //错失先机特性
        public bool stayOneTurn;
        public int subHP;
        public bool substituted;
        public int successContinAttackCounter;

        public bool torment;    //寻衅
        public int toxinCounter;    //剧毒计数器
        public bool uTurn;
        public bool worrySeed;

        #endregion

        #region 克制
        public bool bide;
        public int bideCounter;
        public int bideHurt;
        public PokemonIndex bideTarget;
        #endregion

        #region 不受控的攻击

        public bool CrazyAttack { get; private set; }
        private int cannotControlCounter;
        private int cannotControlTurn;

        /// <summary>
        /// 吵闹
        /// </summary>
        public bool Uproar { get; private set; }

        #endregion

        #region 着迷
        public bool captivated;
        public Pokemon captivateTarget;
        #endregion

        #region 充电
        public bool charge;
        public int chargeCounter;
        #endregion

        #region 混乱计数器
        public bool confusion;
        public int confusionCounter;
        public int confusionTurn;
        #endregion

        #region 暂时束缚
        public bool constraint;
        public PokemonIndex constraintBy;
        public int constraintCounter;
        public int constraintTurn;
        #endregion

        #region 扣押
        public bool detain;
        public int detainCounter;
        #endregion

        #region 石化功
        public int DisableCounter;
        public int DisableIndex;
        public int DisableTurn;
        #endregion

        #region 潜水/钻地/影遁 etc
        public bool dig;
        public bool disappear;
        public bool dive;
        public bool fly;
        public bool jump;

        public bool Hide
        {
            get
            {
                return this.dig || this.disappear || this.dive || this.fly || this.jump;
            }
        }

        #endregion

        #region 鼓掌状态
        public bool encore;
        public int encoreCounter;
        public int encoreTurn;
        #endregion

        #region 未来攻击
        public bool futureAtk;
        public int futureAtkAcc;
        public int futureCounter;
        public int futureDamage;
        #endregion

        #region 回复封印
        public bool healBlock;
        public int healBlockCounter;
        #endregion

        #region 技能->锁定
        public bool lockOn;
        public int lockOnCounter;
        #endregion

        #region 滚动
        private int mRollCounter = 1;
        private bool rollSuccessed;

        private void BeginRoll(Move move)
        {
            this.CrazyAttack = true;
            this.cannotControlTurn = 5;
            this.cannotControlCounter = 0;
            this.mRollCounter = 2;
            this.rollSuccessed = true;
            this.nextMove = move;
        }

        private void NextRoll()
        {
            this.rollSuccessed = true;
            this.mRollCounter++;
        }

        public int RollCounter
        {
            get
            {
                return this.mRollCounter;
            }
        }

        public void Roll(Move move)
        {
            if (this.mRollCounter > 1)
            {
                this.NextRoll();
            }
            else
            {
                this.BeginRoll(move);
            }
        }

        #endregion

        #region 咩歌
        public bool perishSong;
        public int perishSongCounter;
        #endregion

        #region 保护/见切/忍耐
        public bool endure;
        public bool protect;
        public bool protectCancel;
        public int protectCounter;
        #endregion

        #region 寄生种子
        public bool seed;
        public PokemonIndex seedTarget;
        #endregion

        #region 抢夺
        public Pokemon snatchBy;
        public bool snatched;
        #endregion

        #region 能量储存
        public int StockpileCounter;
        public int StockpileDefCounter;
        public int StockpileSDefCounter;
        #endregion

        #region 电磁浮游
        public bool suspension;
        public int suspensionCounter;
        #endregion

        #region 挑拨状态
        public bool taunt;
        public int TauntCounter;
        public int TauntTurn;
        #endregion

        #region temp

        public Bitmap tempImg1;
        public Bitmap tempImg2;
        public Move[] TempMoves = new Move[4];


        public BaseStats TempStats { get; set; }

        public int tempAttack;
        public int tempDefence;
        public int tempSAttack;
        public int tempSDefence;
        public int tempSpeed;

        public Trait tempTrait;

        public BattleType TempType1 { get; set; }
        public BattleType TempType2 { get; set; }
        public BattleType RoostType { get; set; }

        #endregion

        #region 变身
        public bool traitRaised;
        public bool transfrom;
        public PokemonData transTemp;
        #endregion

        #region 许愿
        public bool wished; //技能许愿
        public int wishTurn = -1;
        #endregion

        #region 哈欠
        public bool yawn;
        public int yawnCounter;
        #endregion

        #endregion

        /// <summary>
        /// 2-3次混乱攻击
        /// </summary>
        public void BeginCrazyAttack(Random random, Move move)
        {
            this.CrazyAttack = true;
            this.cannotControlTurn = random.Next(2, 4);
            this.cannotControlCounter = 0;
            this.nextMove = move;
        }

        public void BeginUproar(Random random, Move move)
        {
            this.CrazyAttack = true;
            this.cannotControlTurn = random.Next(2, 6);
            this.cannotControlCounter = 0;
            this.Uproar = true;
            this.nextMove = move;
        }

        List<BaseBattleState> states = new List<BaseBattleState>();

        public void CountOfEndTurn(Pokemon pm, BattleGround ground, ILogBuilder log)
        {
            foreach (var state in states)
            {
                state.CountOfEndTurn(ground, log.AppendText);
            }

            this.afraid = false;
            this.magicCoat = false;
            this.snatched = false;
            this.snatchBy = null;
            this.meFirst = false;
            this.assisted = false;
            if (pm.State == PokemonState.Toxin)
            {
                this.toxinCounter++;
            }
            if (this.perishSong)
            {
                this.perishSongCounter--;
            }
            if (this.constraint && (this.constraintTurn != -1))
            {
                this.constraintCounter++;
            }
            if (this.suspension)
            {
                this.suspensionCounter--;
            }
            if (this.healBlock)
            {
                this.healBlockCounter--;
            }
            if (this.DisableIndex != 0)
            {
                this.DisableCounter++;
            }
            if (this.yawn)
            {
                this.yawnCounter++;
            }
            if (this.detain)
            {
                this.detainCounter--;
            }
            if (this.charge)
            {
                this.chargeCounter--;
            }
            if (this.lockOn)
            {
                this.lockOnCounter--;
            }
            if (this.roost)
            {
                this.roost = false;
                this.RoostType = BattleType.None;
            }
            if (this.protect)
            {
                this.protect = false;
            }
            else if (this.protectCancel)
            {
                this.protectCancel = false;
            }
            else if (this.endure)
            {
                this.endure = false;
            }
            else
            {
                this.protectCounter = 0;
            }
            if (this.charge && (this.chargeCounter == 0))
            {
                this.charge = false;
            }
            if (this.lockOn && (this.lockOnCounter == 0))
            {
                this.lockOn = false;
            }
            if (this.healBlock && (this.healBlockCounter == 0))
            {
                this.healBlock = false;
                log.AppendMsg("DeHealBlock", pm.GetNameString());
                //log.AppendText(pm.GetNameString() + "的回复封印解除了！");
            }
            if (this.detain && (this.detainCounter == 0))
            {
                this.detain = false;
                log.AppendMsg("DeEmbargo", pm.GetNameString());
                //log.AppendText(pm.GetNameString() + "的道具恢复了效果！");
            }
            if (this.CrazyAttack)
            {
                this.cannotControlCounter++;
            }
            if (this.CrazyAttack)
            {
                if (this.cannotControlCounter == this.cannotControlTurn)
                {
                    //log.AppendText(pm.GetNameString() + "停止了攻击！");
                    if (pm.BattleState.Uproar)
                    {
                        pm.BattleState.Uproar = false;
                        log.AppendMsg("DeUproar", pm.GetNameString());
                    }
                    else if (this.mRollCounter < 2)
                    {
                        pm.confuse(false, null);
                        log.AppendMsg("EnConfuse2", pm.GetNameString());
                    }
                    this.CrazyAttack = false;
                    this.rollSuccessed = false;
                    this.mRollCounter = 1;
                    this.nextMove = null;
                }
                else if (this.mRollCounter > 1)
                {
                    if (this.rollSuccessed)
                    {
                        this.rollSuccessed = false;
                    }
                    else
                    {
                        this.mRollCounter = 1;
                        this.CrazyAttack = false;
                    }
                }
            }
            if (this.bide)
            {
                this.bideCounter++;
            }
            if (this.bideCounter == 3)
            {
                pm.BattleState.bide = false;
            }
            if (this.taunt)
            {
                this.TauntCounter++;
                if (this.TauntCounter == this.TauntTurn)
                {
                    this.taunt = false;
                    log.AppendMsg("DeTaunt", pm.GetNameString());
                    //log.AddText(pm.GetNameString() + "的挑拨状态结束了");
                }
            }
            if (this.suspension && (this.suspensionCounter == 0))
            {
                this.suspension = false;
                log.AppendMsg("DeMagnetRise", pm.GetNameString());
                //log.AddText(pm.GetNameString() + "的电磁悬浮状态消失了！");
            }
            if ((this.DisableIndex != 0) && (this.DisableCounter == this.DisableTurn))
            {
                log.AppendMsg("DeDisable", pm.GetNameString());
                //log.AddText(pm.GetNameString() + "的" + pm.GetSelMove(this.DisableIndex).Name + "可以使用了！");
                this.DisableIndex = 0;
            }
            if (this.constraint && (this.constraintTurn == this.constraintCounter))
            {
                this.constraint = false;
                //log.AppendMsg("TrapFree");
                log.AppendText(pm.GetNameString() + "的束缚解除了！");
            }
            if (this.wished && (this.wishTurn == 1))
            {
                this.wished = false;
                log.AppendMsg("Wish", pm.MyTeam.PlayerName);
                //log.AddText(pm.MyTeam.GetPlayerName(pm) + "队伍的愿望实现了！");
                if (pm.HealthPower < pm.MaxHp)
                {
                    pm.HPRecoverFraction(2);
                    log.AppendMsg("HpRecover", pm.GetNameString());
                    //log.AddText(pm.GetNameString() + "恢复了HP！");
                }
                else
                {
                    log.AppendMsg("FullHp", pm.GetNameString());
                    //log.AddText(pm.GetNameString() + "的HP已经满了！");
                }
            }
            if (this.encore)
            {
                this.encoreCounter++;
                if ((this.encoreTurn == this.encoreCounter) || ((pm.lastMoveIndex != 5) && (pm.GetSelMove(pm.lastMoveIndex).PP == 0)))
                {
                    log.AppendMsg("DeEncore", pm.GetNameString());
                    //log.AddText(pm.GetNameString() + "的鼓掌结束了！");
                    this.encore = false;
                }
            }
            if (this.yawn && (this.yawnCounter == 2))
            {
                pm.SetState(PokemonState.Sleep, null, true, false, false, false, false);
                this.yawn = false;
            }
        }

        /// <summary>
        /// 接力用
        /// </summary>
        public void PassCopy(PokemonBattleState state)
        {
            this.CTLV = state.CTLV;
            this.FocusEngery = state.FocusEngery;

            //this.AccLV = state.AccLV;
            //this.evasionLV = state.evasionLV;
            //this.AttackLV = state.AttackLV;
            //this.DefenceLV = state.DefenceLV;
            //this.SpeedLV = state.SpeedLV;
            //this.SpAttackLV = state.SpAttackLV;
            //this.SpDefenceLV = state.SpDefenceLV;

            this.AbilityLv = (BattleStats)state.AbilityLv.Clone();

            this.substituted = state.substituted;
            this.subHP = state.subHP;
            this.lockOn = state.lockOn;
            this.constraint = state.constraint;
            this.constraintTurn = state.constraintTurn;
            this.constraintCounter = state.constraintCounter;
            this.constraintBy = state.constraintBy;
            this.confusion = state.confusion;
            this.confusionTurn = state.confusionTurn;
            this.confusionCounter = state.confusionCounter;
            this.perishSong = state.perishSong;
            this.perishSongCounter = state.perishSongCounter;
            this.suspension = state.suspension;
            this.suspensionCounter = state.suspensionCounter;
            this.detain = state.detain;
            this.detainCounter = state.detainCounter;
            this.curse = state.curse;
            this.ingrain = state.ingrain;
            this.aquaRing = state.aquaRing;
            this.gastricJuice = state.gastricJuice;
            this.powerTrick = state.powerTrick;
            this.seed = state.seed;
            this.seedTarget = state.seedTarget;

            state = null;
        }

    }
}
